import {DefaultFieldAction, DefaultFormDefinition, DefaultViewAction} from '@/components/form/DefaultFormDefinition';
import {guid, isEmpty, isNotEmpty, resolveSort} from '@/utils/CommonUtils';
import FormDefinitionHelper from '@/components/form/FromDesigner/FormDefinitionHelper';

export default class {
  formDefinition = DefaultFormDefinition;
  formDefinitionHelper;
  viewActions = [];

  constructor(formDefinition) {
    this.formDefinition = formDefinition;
    this.viewActions = formDefinition.viewActions;
    this.formDefinitionHelper = new FormDefinitionHelper(formDefinition);

    this.refreshViews();
  }

  refreshViews() {
    this.removeRedundantViewActions();

    this.createLackViewActions();

    this.createDefaultViewActions();

    this.removeRedundantFieldActions();

    this.createLackFieldActions();

    this.removeRedundantSubFormButtonActions();

    this.createLackSubFormButtonActions();

    this.resolveFormMenuActionRange();

    this.resolveFormMenuButtons();
  }

  removeRedundantViewActions() {
    let {views, viewActions} = this.formDefinition;
    this.formDefinition.viewActions = viewActions.filter(viewAction => {
      let index = views.findIndex(v => v.id === viewAction.viewId);
      return index !== -1;
    });
  }

  createLackViewActions() {
    let {views, viewActions} = this.formDefinition;
    views.forEach(view => {
      let viewAction = viewActions.find(viewAction => viewAction.viewId === view.id);
      if (isEmpty(viewAction)) {
        viewAction = this.createDefaultViewActionByView(view);
        viewActions.push(...viewAction);
      }
    });
  }

  createDefaultViewActionByView(view) {
    let res = [];
    res.push(this.createDefaultEditAction(view.id));
    res.push(this.createDefaultApproveAction(view.id));
    res.push(this.createDefaultShowAction(view.id));
    return res;
  }

  createDefaultEditAction(viewId) {
    let res = this.createEmptyViewAction();
    res.systemPreset = '1';
    res.actionName = '填写';
    res.actionType = 'edit';
    res.viewId = viewId;
    let fieldActions = this.createFieldActoins();
    fieldActions.forEach(action => {
      action.defaultAuth = 'edit';
    });
    res.fieldActions = fieldActions;
    return res;
  }

  createDefaultApproveAction(viewId) {
    let res = this.createEmptyViewAction();
    res.systemPreset = '1';
    res.actionName = '审批';
    res.actionType = 'approve';
    res.viewId = viewId;
    let fieldActions = this.createFieldActoins();
    fieldActions.forEach(action => {
      action.defaultAuth = 'browse';
    });
    res.fieldActions = fieldActions;
    return res;
  }

  createDefaultShowAction(viewId) {
    let res = this.createEmptyViewAction();
    res.systemPreset = '1';
    res.actionName = '显示';
    res.actionType = 'show';
    res.viewId = viewId;
    let fieldActions = this.createFieldActoins();
    fieldActions.forEach(action => {
      action.defaultAuth = 'browse';
    });
    res.fieldActions = fieldActions;
    return res;
  }

  createFieldActoins() {
    let fieldVos = this.formDefinitionHelper.getFieldVos();
    if (isEmpty(fieldVos)) fieldVos = [];
    let res = [];
    fieldVos.forEach(fieldVo => {
      let temp = this.createFieldActionByFieldVo(fieldVo);
      res.push(temp);
    });
    resolveSort(res);
    return res;
  }

  createFieldActionByFieldVo(fieldVo) {
    let temp = this.createEmptyFieldAction();
    temp.formName = fieldVo.formName;
    temp.formField = fieldVo.formField;
    temp.fieldShowName = fieldVo.formFieldVariableShowName;
    temp.fieldComponent = fieldVo.fieldComponent;
    return temp;
  }

  createEmptyFieldAction() {
    let res = JSON.parse(JSON.stringify(DefaultFieldAction));
    res.id = guid();
    return res;
  }

  createEmptyViewAction() {
    let res = JSON.parse(JSON.stringify(DefaultViewAction));
    res.id = guid();
    res.actionKey = guid();
    return res;
  }

  createDefaultViewActions() {
    let {views, viewActions} = this.formDefinition;
    views.forEach(view => {
      let viewActionsTemp = viewActions.filter(viewAction => viewAction.viewId === view.id);
      let editAction = viewActionsTemp.find(v => v.systemPreset + '' === '1' && v.actionType === 'edit');
      if (isEmpty(editAction)) viewActions.push(this.createDefaultEditAction(view.id));

      let approveAction = viewActionsTemp.find(v => v.systemPreset + '' === '1' && v.actionType === 'approve');
      if (isEmpty(approveAction)) viewActions.push(this.createDefaultApproveAction(view.id));

      let showAction = viewActionsTemp.find(v => v.systemPreset + '' === '1' && v.actionType === 'show');
      if (isEmpty(showAction)) viewActions.push(this.createDefaultEditAction(view.id));
    });
  }

  removeRedundantFieldActions() {
    let fieldVos = this.formDefinitionHelper.getFieldVos();
    let {views, viewActions} = this.formDefinition;
    viewActions.forEach(viewAction => {
      let {fieldActions} = viewAction;
      viewAction.fieldActions = fieldActions.filter(fieldAction => {
        let vo = fieldVos.find(vo => vo.formName === fieldAction.formName && vo.formField === fieldAction.formField);
        return isNotEmpty(vo);
      });
    });
  }

  createLackFieldActions() {
    let {viewActions} = this.formDefinition;
    let fieldVos = this.formDefinitionHelper.getFieldVos();

    viewActions.forEach(viewAction => {
      let {fieldActions} = viewAction;
      fieldVos.forEach(vo => {
        let fieldAction = fieldActions.find(fieldAction =>
          fieldAction.formName === vo.formName && fieldAction.formField === vo.formField);


        if (isEmpty(fieldAction)) {
          let action = this.createFieldActionByFieldVo(vo);

          if (viewAction.actionType === 'edit') {
            action.defaultAuth = 'edit';
          }
          if (viewAction.actionType === 'approve') {
            action.defaultAuth = 'browse';
          }
          if (viewAction.actionType === 'show') {
            action.defaultAuth = 'browse';
          }
          fieldActions.push(action);
        } else {
          fieldAction.fieldShowName = vo.formFieldVariableShowName;
        }
      });
    });
  }

  removeRedundantSubFormButtonActions() {
    let viewActions = this.formDefinition.viewActions;
    viewActions.forEach(viewAction => {
      let {subFormButtonAction} = viewAction;
      if (isEmpty(subFormButtonAction))
        viewAction.subFormButtonAction = subFormButtonAction = [];
      let {subForms} = this.formDefinition;
      if (isEmpty(subForms)) subForms = [];

      subFormButtonAction = subFormButtonAction.filter(buttonAction => {
        let subForm = subForms.find(subForm => subForm.id === buttonAction.subFormId);
        return isNotEmpty(subForm);
      });
      viewAction.subFormButtonAction.splice(0, viewAction.subFormButtonAction.length);
      viewAction.subFormButtonAction.push(...subFormButtonAction);
    });
  }

  createLackSubFormButtonActions() {
    let viewActions = this.formDefinition.viewActions;
    viewActions.forEach(viewAction => {
      let {subFormButtonAction} = viewAction;
      if (isEmpty(subFormButtonAction))
        viewAction.subFormButtonAction = subFormButtonAction = [];
      let {subForms} = this.formDefinition;
      if (isEmpty(subForms)) subForms = [];

      subForms.forEach(subForm => {
        let buttonAction = subFormButtonAction.find(buttonAction => buttonAction.subFormId === subForm.id);
        if (isEmpty(buttonAction)) {
          let buttonAction = this.createDefaultViewActionSubFormButtonAction(viewAction, subForm);
          subFormButtonAction.push(buttonAction);
        }
      });
    });
  }

  resolveFormMenuButtons() {
    let menus = this.formDefinition.menus;
    if (isEmpty(menus)) return;
    menus.forEach(menu => {
      let menuButtons = menu.menuButtons;
      if(isEmpty(menuButtons)) menuButtons = []
      menuButtons.forEach(button => {
        if (button.buttonActionType === 'edit') {
          button.supportMultiple = false;
        }

        if (button.buttonActionType === 'detail') {
          button.supportMultiple = false;
        }

        if (button.buttonActionType === 'createForm') {
          button.supportMultiple = true;
        }

        if (button.buttonActionType === 'relateForm') {
          this.resolveRelateFormSupportMultiple(button);
        }
      })
      resolveSort(menuButtons);
    });

  }

  resolveRelateFormSupportMultiple(menu) {
    let relations = menu.relations;
    if (isEmpty(relations)) relations = [];
    let flag = true;
    relations.forEach(rel => {
      if (rel.targetMainField) {
        flag = false;
      }
    });
    menu.supportMultiple = flag;
  }

  resolveFormMenuActionRange() {
    let menus = this.formDefinition.menus;
    if (isEmpty(menus)) return;
    menus.forEach(menu => {
      if (isEmpty(menu) || isEmpty(menu.menuActionRanges)) return;
      let menuActionRanges = menu.menuActionRanges;
      if (isEmpty(menuActionRanges)) return;
      resolveSort(menuActionRanges);
    });
  }

  createDefaultViewActionSubFormButtonAction(viewAction, subForm) {
    let res = {
      id: guid(),
      subFormId: subForm.id,
      viewActionId: viewAction.id,
      defaultAddButtonAuth: '0',
      defaultRemoveButtonAuth: '0',
      defaultMinLines: 0,
    };

    if (viewAction.actionType === 'edit') {
      res.defaultAddButtonAuth = '1';
      res.defaultRemoveButtonAuth = '1';
    }
    return res;
  }
};
